// 导入 createSlice 和 PayloadAction 模块
import { createSlice, PayloadAction } from '@reduxjs/toolkit';

// 定义用户状态接口
interface UserState {
  userInfo: {
    id?: string;
    username?: string;
    email?: string;
  } | null;
  loading: boolean;
  error: string | null;
}

// 初始化用户状态
const initialState: UserState = {
  userInfo: null,
  loading: false,
  error: null,
};

// 创建用户切片
const userSlice = createSlice({
  name: 'user',
  initialState,
  reducers: {
    setUserInfo: (state, action: PayloadAction<UserState['userInfo']>) => {
      state.userInfo = action.payload;
    },
    setLoading: (state, action: PayloadAction<boolean>) => {
      state.loading = action.payload;
    },
    setError: (state, action: PayloadAction<string | null>) => {
      state.error = action.payload;
    },
  },
});

// 导出用户切片操作
export const { setUserInfo, setLoading, setError } = userSlice.actions;

// 导出用户切片 reducer
export default userSlice.reducer; 